<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>socket消息</title>
    <script src="js/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="myModal">

</div>
</body>
<script>
    var websocket=null;
    if('WebSocket' in window){
        websocket=new WebSocket('ws://localhost:8081/webSocket');
    }else{
        alert('该浏览器不支持websocket');
    }
    websocket.onopen=function (ev) {
        console.log('建立连接');
    }
    websocket.onclose=function (ev) {
        console.log('连接关闭');
    }
    websocket.onmessage=function (ev) {
        console.log('收到消息：'+ev.data);
        //弹窗提醒，播放消息
        $('#myModal').append(ev.data+'</br>')
    }
    window.onbeforeunload=function (ev) {
        websocket.close();
    }
</script>
</html>